package com.ruoyi.yl.rocketmq.consumer;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.yl.process.ActivityProcess;
import com.ruoyi.common.domain.constant.enums.MessTopicAndTag;
import com.ruoyi.common.domain.constant.to.MessageActivityTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;


@Service
@Slf4j
@RocketMQMessageListener(
        topic = "yl_activity",
        consumerGroup = "yl-consumer-activity-group",
        selectorExpression = "*"  // 控制tag消费 在后面拼接  消费所有Tag的消息
)
public class ActivityConsumer implements RocketMQListener<MessageExt> {


    private final ActivityProcess activityProcess;



    public ActivityConsumer( ActivityProcess activityProcess) {
        this.activityProcess = activityProcess;

    }

    @Override
    public void onMessage(MessageExt messageExt) {
        MessageActivityTo to = JSON.parseObject(messageExt.getBody(), MessageActivityTo.class);

        if (MessTopicAndTag.YL_ACTIVITY_INCREASE_PARTICIPATE.getTag().equals(messageExt.getTags())){
            try{
                activityProcess.increaseParticipateByActivityId(to);
            }catch (Exception e){
                log.error("消费消息失败，将自动重试", e);
                throw new RuntimeException("消费失败", e);
            }

        }

    }
}
